package com.hs.srm.admin.modules.sampletest.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.orders.entity.ScmPoItemEntity;
import com.hs.srm.admin.modules.orders.vo.ScmPoItemVo;
import com.hs.srm.admin.modules.sampletest.dao.SampleScmPoItemDao;
import com.hs.srm.admin.modules.sampletest.entity.UploadFileRemarkEntity;
import com.hs.srm.admin.modules.sampletest.service.SampleScmPoItemService;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.springframework.stereotype.Service;
import sunnykid.text.SunnykidText;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 样品试用，采购订单行项目Service实现
 *
 * @author ych
 *
 * @date 2019-7-3
 */
@Service("sampleScmPoItemService")
public class SampleScmPoItemServiceImpl  extends ServiceImpl<SampleScmPoItemDao, UploadFileRemarkEntity> implements SampleScmPoItemService {

    //样品试用，订单行项目数据
    @Override
    public PageUtils selectDetailPage(Map<String, Object> params) {
        List<String> ids = new ArrayList<String>();
        List<String> status = new ArrayList<String>();
        String idss[] = null;
        String statuss[] = null;
        String idsstr="";
        String statusstr="";
        if(params.get("ids")!=null){
            idsstr = params.get("ids").toString();
            idss = idsstr.split(",");
            for(int i=0;i<idss.length;i++){
                ids.add(idss[i]);
            }
            params.put("ids", ids);
        }
        if(params.get("status")!=null){
            statusstr = params.get("status").toString();
            statuss = statusstr.split(",");
            for(int i=0;i<statuss.length;i++){
                if(statuss[i].equals("-1")||statuss[i].equals("0")){
                    status.add(statuss[i]);
                }
            }
            params.put("status", status);
        }

        Page<ScmPoItemVo> page = this.selectDetailPage(new QueryParam<ScmPoItemVo>(params).getPage(),
                new EntityWrapper<ScmPoItemVo>(),params);
        return new PageUtils(page);
    }

    private Page<ScmPoItemVo> selectDetailPage(Page<ScmPoItemVo> page, Wrapper<ScmPoItemVo> wrapper,
                                               Map<String, Object> params) {
        page.setRecords(this.baseMapper.selectDetailPage(page, wrapper, params));
        return page;
    }

    //保存上传文件路径，备注
    @Override
    public boolean saveUploadPathRemark(HttpServletRequest request, String filePath) {
        UploadFileRemarkEntity ssp=setSampleEntity(request,filePath);
        boolean result=insertOrUpdate(ssp);
        return result;
    }

    //设置保存数据entity
    private UploadFileRemarkEntity setSampleEntity(HttpServletRequest request, String filePath) {
        UploadFileRemarkEntity ssp= new UploadFileRemarkEntity();
        //备注
        String remark= request.getParameter("remark");
        //上传类型
        String uploadType= request.getParameter("uploadType");
        //上传文件标记
        int uploadFlag=Integer.parseInt(request.getParameter("uploadFlag"));
        //行项目id
        String poItemId=request.getParameter("poItemId");
        ssp.setPoItemId(poItemId);
        //上传文件表id
        String uploadId=request.getParameter("uploadId");
        if(uploadId==null||"".equals(uploadId)){
            uploadId= SunnykidText.getUUID();
        }
        ssp.setUploadId(uploadId);
        return ssp;
    }

}
